package net.cyndeline.GUI.graphics

import javafx.scene.image.{Image, ImageView}
import net.cyndeline.GUI.setup.AnimationType
import net.cyndeline.misc.Bag

/**
 * Contains the graphical representation for a single entity or effect on the game map. Allows the game to invoke
 * methods in order to change the look of the entity by triggering animations and positioning.
 */
class EntityRepresentation(defaultAnimation: Animation) extends Representation {
  private val animations = new Bag[Animation]()
  private var currentAnimation: Animation = defaultAnimation

  /* The pixel coordinates on the x/y axises where this representation should be rendered. */
  override var x = 0
  override var y = 0

  /** Retrieves the current sprite representation of this entity. */
  override def getView: Image = currentAnimation.currentSprite

  /** Activates an animation of a specified type, or defaults to an animation if the type isn't found. */
  override def switchToAnimation(animation: AnimationType) {
    currentAnimation = animations.get(animation.id).getOrElse {
      defaultAnimation
    }
  }

  /** Should be called every frame to advance the animation (if any) of this representation. */
  override def process() {
    currentAnimation.update()
  }

  /** Height in pixels of each animation frame. */
  override def height: Int = defaultAnimation.frameHeight.toInt

  /** Width in pixels of each animation frame. */
  override def width: Int = defaultAnimation.frameWidth.toInt

  /** Adds an animation of a specified type. */
  def addAnimation(animation: Animation, t: AnimationType) {
    if (animations.get(t.id).isDefined) {
      throw new Error("Cannot add an animation of type " + t.getClass + " twice.")
    } else if (animation.frameHeight != defaultAnimation.frameHeight || animation.frameHeight != defaultAnimation.frameHeight) {
      //TODO test
      throw new IllegalArgumentException("Cannot add animations of different frame size to the same representation.")
    }
    animations.set(t.id, animation)
  }

}
